# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：createGif.py
#日期：2019-05-24
#备注：通过imageio库制作GIF动图    pip install imageio
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import imageio

import os

dir = './gif'     # 图片存储路径
def create_gif(image_list,gif_name,duration = 1.0):
    '''
    :param image_list: 这个列表用于存放生成动图的图片(可以是jpg或者png)
    :param gif_name: 字符串，所生成gif文件名，带.gif后缀
    :param duration: 图像间隔时间
    :return:
    '''
    os.chdir(dir)
    images = []
    for image in image_list:
        images.append(imageio.imread(image))
    imageio.mimsave(gif_name,images,'GIF',duration=duration)
    return


def imageio_gif(gif_file):
    os.chdir(dir)
    img = imageio.imread(gif_file)
    #print(img)
    img0 = img.copy()
    img1 = img.copy()
    img0[:, :, 1] = 0
    img0[:, :, 2] = 0
    img1[:, :, 0] = 0
    img1[:, :, 2] = 0
    imageio.imwrite('x0.png',img0)
    imageio.imwrite('x1.png', img1)
    reader = imageio.get_reader('1.mp4','ffmpeg')
    print(reader)
    fps = reader.get_meta_data()['fps']
    writer = imageio.get_writer('gray.mp4', fps=fps)
    for im in reader:
        print(1,im)
        writer.append_data(im[:, :, 2])
    writer.close()


def main():
    files = os.listdir(dir)
    image_list = [f for f in files if f.endswith((".png"))]
    gif_name = 'out.gif'
    duration = 0.05
    #print(image_list)
    create_gif(image_list, gif_name, duration)


if __name__ == '__main__':
    imageio_gif('1.png')
    #split_gif('test.gif')
    #main()